package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜单相关接口")
public class DishController {

    @Autowired
    private DishService dishService;


    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dish) {
        log.info("添加新菜品:{}", dish);
        dishService.addDish(dish);

        //清除该分类的redis缓存
        String key = "dish_"+dish.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    /**
     * 分页展示菜品
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页展示菜品")
    @GetMapping("/page")
    public Result<PageResult> getDishList(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.getDish(dishPageQueryDTO);
        log.info(pageResult.toString());
        return Result.success(pageResult);
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<DishVO>> dishList(String categoryId) {
        log.info("根据分类id查询菜品:{}",categoryId);
        List<DishVO> dishVOList = dishService.getDishByCategoryId(categoryId);
        return Result.success(dishVOList);
    }

    /**
     * 根据菜品id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据菜品id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishByDishId(@PathVariable Long id) {
        log.info("根据菜品id查询菜品");
        DishVO dishVO = dishService.getDishByDishId(id);
        log.info(dishVO.toString());
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品{}",dishDTO);
        dishService.update(dishDTO);

        //如果只修改菜品名称或价格，那只需要删除一个分类的缓存，如果修改的是分类就会影响两个分类缓存数据
        //又因为这个操作不经常使用，所以这里直接全部删除

        //直接批量删除dish_开头的全部缓存数据
        //先查询出来所有的key
        Set keys = redisTemplate.keys("dish_*");
        //然后再删除，支持set集合删除
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 修改菜品的状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改菜品状态")
    @PostMapping("status/{status}")
    public Result updateDishStatus(@PathVariable("status") Integer status,Long id) {
        log.info("修改菜品{}的状态为{}",id,status);
        dishService.updateDishStatus(status,id);

        Set keys = redisTemplate.keys("dish_*");
        //然后再删除，支持set集合删除
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 删除菜品和菜品下的口味
     * @param str
     * @return
     */
    @ApiOperation("删除菜品和菜品下的口味")
    @DeleteMapping
    public Result deleteDish(@RequestParam("ids") String str) {

        String[] ids = str.split(",");
        log.info("删除菜品id为{}", Arrays.toString(ids));
        dishService.deleteDish(ids);

        //直接批量删除dish_开头的全部缓存数据
        //先查询出来所有的key
        Set keys = redisTemplate.keys("dish_*");
        //然后再删除，支持set集合删除
        redisTemplate.delete(keys);

        return Result.success();
    }

}
